// pages/fastAddInsuranceStatus/fastAddInsuranceStatus.js
const app = getApp()
import {
    fastAddInsuranceApi
} from '../../api/fastAddInsurance'
import user from '../../utils/user'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        status: '',
        title: '',
        msg: '',
        info: {},
        options: {}
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        console.log(2121, options)
        this.setData({
            options
        })
    },
    login() {
        user.goRegister(false, '/pages/fastAddInsuranceStatus/fastAddInsuranceStatus?scene=' + this.data.options.scene)
    },
    onGoHome() {
        wx.switchTab({
            url: '/pages/index/index',
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {
        // const query = wx.createSelectorQuery()
        // query.select('#myCanvas')
        //     .fields({
        //         node: true,
        //         size: true
        //     })
        //     .exec((res) => {
        //         console.log(res, 'sfsfg')
        //         const canvas = res[0].node
        //         const ctx = canvas.getContext('2d')
        //         const dpr = wx.getSystemInfoSync().pixelRatio
        //         canvas.width = res[0].width * dpr
        //         canvas.height = res[0].height * dpr
        //         ctx.scale(dpr, dpr)
        //         ctx.fillRect(0, 0, 200, 200)
        //         ctx.fillStyle = 'rgba(0, 0, 200, 0.5)'
        //         ctx.fillRect(30, 30, 50, 50)
        // })
    },

    handleToRegister() {
        wx.showModal({
            title: '提示',
            content: '您还未注册，是否立即前往注册？',
            complete: (res) => {
                console.log(res)
                if (res.cancel) {
                    wx.switchTab({
                        url: '/pages/index/index',
                    })
                }
                if (res.confirm) {
                    this.login()
                }
            }
        })
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        console.log(11111, this.data.options.scene, decodeURIComponent(this.data.options.scene).split('=')[1])
        user.waitLogin().then(async () => {
            console.log(2)
            if (!user.userId) {
                this.handleToRegister()
                return
            }
            if (!user.userName || !user.cardNo) {
                wx.showModal({
                    title: '提示',
                    content: '请完成实名',
                    complete: (res) => {
                        if (res.cancel) {
                            wx.switchTab({
                                url: '/pages/index/index',
                            })
                        }
                        if (res.confirm) {
                            wx.redirectTo({
                                url: '/pages/personal-info/personal-info',
                            })
                        }
                    }
                })
                return
            }
            let sceneData = ''
            if (this.data.options.scene) {
                sceneData = this.data.options.scene
            } else if(this.data.options.q) {
                let decodeData = decodeURIComponent(this.data.options.q)
                const scene = decodeData.split('&')[1]
                sceneData = scene.split('=')[1]
            }
            let res = await fastAddInsuranceApi({
                qrProjectId: decodeURIComponent(sceneData).split('=')[1],
                staffRecord: {
                    userName: user.userName,
                    idCardNo: user.cardNo
                }
            })
            console.log(res, 'bdfghsuhugdhu')
            if (res.code === 1) {
                this.setData({
                    status: 3,
                    title: '投保失败',
                    msg: res.msg
                })
            } else if (res.code === 3) {
                this.setData({
                    status: 4,
                    title: '投保失败',
                    msg: res.msg,
                    info: res.record
                })
            } else {
                if (res.record.policyStatus === 0) {
                    this.setData({
                        status: 1,
                        title: '投保申请已提交',
                        msg: '您已提交申请，待' + res.record.insurStartTime + '后保险即可生效'
                    })
                } else if (res.record.policyStatus === 1) {
                    this.setData({
                        status: 2,
                        title: '保障中',
                        msg: '投保成功，当前保险已生效'
                    })
                }
                this.setData({
                    info: res.record
                })
            }
        }).catch(err => {
            if (err === undefined) {
                this.handleToRegister()
            }
        })
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})